package container

import "reflect"

type Container struct {
	beansByName  map[string]interface{}
	beansByIface map[reflect.Type]interface{}
	config       map[string]string
}

func New(config map[string]string) *Container {
	return &Container{
		beansByName:  make(map[string]interface{}),
		beansByIface: make(map[reflect.Type]interface{}),
		config:       config,
	}
}

func (c *Container) RegisterBean(name string, iface interface{}, impl interface{}) {
	c.beansByName[name] = impl
	if iface != nil {
		t := reflect.TypeOf(iface).Elem()
		c.beansByIface[t] = impl
	}
}

func (c *Container) GetBeanByName(name string) interface{} {
	return c.beansByName[name]
}

func (c *Container) GetBeanByInterface(ifacePtr interface{}) interface{} {
	t := reflect.TypeOf(ifacePtr).Elem()
	return c.beansByIface[t]
}
